package edu.gatech.mobile.scene.image.util;

import java.util.ArrayList;
import java.util.List;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfFloat;
import org.opencv.core.MatOfInt;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;

import edu.gatech.mobile.scene.AppConstants;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.util.Log;

public class HistogramCompareUtil {
	
	private static int[] channelSize = {0};
	private static MatOfInt channelNum = new MatOfInt(channelSize);	
	private static int[] binSize = {AppConstants.COLOR_HISTOMGRAM_BIN_SIZE};
	private static MatOfInt binNum = new MatOfInt(binSize);
	private static float[] colorRangeVals ={0.0f, 256.0f};
	private static MatOfFloat colorRanges = new MatOfFloat(colorRangeVals);
	
	
	
	public static List<Mat> RGBHistCalc(Mat imageMat, Mat grayMask) {
			List<Mat> histograms = new ArrayList<Mat>();
			Imgproc.cvtColor(imageMat, imageMat, Imgproc.COLOR_BGRA2BGR);
			List<Mat> images = new ArrayList<Mat>(); 
			Core.split(imageMat, images);
			
			for(Mat mat : images){
				try{
					List<Mat> inputMat = new ArrayList<Mat>();
					inputMat.add(mat);
					Mat hist = new Mat();
					Imgproc.calcHist(inputMat, channelNum, grayMask, hist,binNum, colorRanges, false);
					histograms.add(hist);
				}
				catch (Exception e) {
					Log.d("JW", e.getMessage());
				}
			}
			return histograms;
	}
	
	
	
	
/*	
public static List<Mat> RGBHistCalcROI(Mat origMat, int maskCol, int maskRow) {
		
		Rect maskRect= new Rect(maskRow, maskCol, AppConstants.BLOCK_SIZE, AppConstants.BLOCK_SIZE);
		Mat imageMat = new Mat(origMat, maskRect);
		Mat maskMat = new Mat();
		
		
		List<Mat> channels = new ArrayList<Mat>();
		for (int i = 0; i < imageMat.channels(); i++) {
			Mat cha = new Mat(imageMat.height(), imageMat.width(),CvType.CV_8UC1);
			channels.add(cha);
		}
		Core.split(imageMat, channels);
						
		
		Mat hist = new Mat();
		List<Mat> histograms = new ArrayList<Mat>();
		
		//Mat null_mask = new Mat();
		
		for(int i=0; i<channels.size()-1; i++){
			
			List<Mat> calcImage = new ArrayList<Mat>();
			calcImage.add(channels.get(i));
			List<Integer> calcChannels = new ArrayList<Integer>();
			calcChannels.add(0);
			List<Float> calcRanges = new ArrayList<Float>();
			calcRanges.add(0.0F);
			calcRanges.add(256.0F);
			List<Integer> calcSizes = new ArrayList<Integer>();
			calcSizes.add(20);
			Imgproc.calcHist(calcImage, channelNum, maskMat, hist,binNum, colorRanges);
			
			Scalar sumScalar = Core.sumElems(hist);
			double sum = sumScalar.val[0];

			Mat normalizer = getNormalizer(hist.height(),hist.width(), sum, hist.type());
			Mat normalizedHist =new Mat(hist.height(), hist.width(),hist.type());
			
			Core.divide(hist, normalizer, normalizedHist);
			histograms.add(normalizedHist);
			
		}
		imageMat.release();
		maskMat.release();
		hist.release();
		
		return histograms;
		
	}
*/
	

	/***
	 * Takes a JPEG captured by the device camera and converts it to RGB888
	 * format
	 */	
	private static Bitmap JPEGtoRGB888(Bitmap img) {
		int numPixels = img.getWidth() * img.getHeight();
		int[] pixels = new int[numPixels];
		// Get JPEG pixels. Each int is the color values for one pixel.
		img.getPixels(pixels, 0, img.getWidth(), 0, 0, img.getWidth(), img
				.getHeight());
		// Create a Bitmap of the appropriate format.
		Bitmap result = Bitmap.createBitmap(img.getWidth(), img.getHeight(),
				Config.ARGB_8888);
		// Set RGB pixels.
		result.setPixels(pixels, 0, result.getWidth(), 0, 0, result.getWidth(),
				result.getHeight());
		return result;
	}

	public static Mat getNormalizer(int height, int width, double value, int cvType){
		double data [] = {value};
		Mat normalizer =new Mat(height, width, cvType);
		for(int j=0; j<normalizer.cols(); j++){
			for(int k=0; k<normalizer.rows(); k++){
				normalizer.put(k, j, data);
			}
		}
		return normalizer;
	}
	
}
